﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Drawing;
using Website.Logger;
using DAL;
using BOL;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;


namespace UILGestion.ObraAdmin.Presupuesto
{
    public partial class RecursoGrid : System.Web.UI.UserControl
    {

        public RecursoGrid()

        {
            bol = new PresupuestoBL();
        }
        private PresupuestoBL bol;

        public GridView itemGrid{ 
            get
        {
            return this.itemGrid;
        }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Load(string idPadre, int idestado, bool bitViewPresupuestoEditEver)
        {
            this.idParent.Value = idPadre;
            this.idEstado.Value = idestado.ToString();
            this.bitViewPresupuestoEditEver.Value = bitViewPresupuestoEditEver.ToInt().ToString();
            BindDataItem();
        }

        public void updateTotalesview()
        {




            //if (this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.ID == "PartidaItem2gridCrtl1")
            //{

            //}
            var a=((UILGestion.ObraAdmin.Presupuesto.PresupuestoReadForm)this.Page);
            a.setestadistica();

            if (this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.ID == "PartidaGrid1")
            {
               PartidaGrid partidaItemGridCrtl1 =
               (PartidaGrid)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;

                int index = ((GridViewRow)this.Parent.Parent.Parent.Parent.Parent).RowIndex;
                partidaItemGridCrtl1.BindDataItem();
                partidaItemGridCrtl1.MonstrarDetalle(index, idParent.Value);
            }
            
        }

        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

                var rendicionItem = DbUtil.Db.Recursos.SingleOrDefault(p => p.idInternal == idInternal);

                bol.deleteRecurso(idInternal);
                
                updateTotalesview();
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void AddItem(object sender, EventArgs e)
        {
            try
            {
                Button lnkdivrecurso = (Button)sender;

                TextBox codigoTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("codigoTxt"));
                TextBox idinsumoTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("idInsumoTXT"));
                
                TextBox cantidadTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("cantidadTXT"));
                TextBox totalTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("totalTXT"));
              

                
               
                dbGestionDataContext _db=new dbGestionDataContext(Parametros.strConnString);
                
                bol.AddRecurso(_db, idinsumoTxt.Text.ToInt(), cantidadTxt.Text.ToDecimal().Value,
                    totalTxt.Text.ToDecimal().Value, idParent.Value.ToInt());
                
                updateTotalesview();
                BindDataItem();
                // GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                

                GridView gv = ((GridView)sender);

                TextBox idInsumoTxt =
                   ((TextBox)gv.Rows[e.RowIndex].FindControl("idInsumoTxt"));
                
                TextBox cantidadTxt =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("cantidadTXT"));
                
                TextBox totalTxt =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("totalTxt"));


                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());
                
                
                
                int idEmpleadoEmisor = int.Parse(System.Web.HttpContext.Current.Session["idEmpleado"].ToString());
               dbGestionDataContext _db=new dbGestionDataContext(Parametros.strConnString);
                bol.UpdateRecurso(_db,idinternal,cantidadTxt.Text.ToDecimal().Value,totalTxt.Text.ToDecimal().Value,idParent.Value.ToInt());

                gv.EditIndex = -1;
                itemGridView.FooterRow.Visible = true;
                updateTotalesview();
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);
                gv.EditIndex = e.NewEditIndex;
                BindDataItem();
                gv.FooterRow.Visible = false;
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        public void BindDataItem()
        {
           


            itemGridView.DataSource = 
                bol.getListRecurso(idParent.Value.ToInt()).ToList();


            if (idEstado.Value.ToInt() == 26 & (bitViewPresupuestoEditEver.Value== "0")) //acabado// or promosion
            {
                itemGridView.ShowFooter = false;
                itemGridView.Columns[8].Visible = false;
                itemGridView.Columns[9].Visible = false;
                itemGridView.DataBind();
            }
            else
            {
                itemGridView.Columns[8].Visible = true;
                itemGridView.Columns[9].Visible = true;
                itemGridView.DataBind();
                resetEmpty(itemGridView);
                setFooter();
            }
            

        }
        private void setFooter()
        {
            //DropDownList tipodocumentoDDL =
            //    ((DropDownList)itemGridView.FooterRow.FindControl("tipodocumentoDDL"));

            
            //tipodocumentoDDL.DataTextField = "descripcion";
            //tipodocumentoDDL.DataValueField = "idInternal";
            // tipodocumentoDDL.DataSource = new ListValueBL().GetDocumentoTipoRecepcionDoc();
            //tipodocumentoDDL.DataBind();
        }
        protected void CancelItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);

                gv.EditIndex = -1;
                gv.FooterRow.Visible = true;
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        private void resetEmpty(GridView gv)
        {
            if (gv.Rows.Count == 0)
            {
                List<RecursoEditInfo> itemList0 = new List<RecursoEditInfo>();
                RecursoEditInfo itemcero = new RecursoEditInfo();
                

                itemList0.Add(itemcero);
                gv.DataSource = itemList0;
                gv.DataBind();
                gv.Rows[0].Visible = false;
            }

        }

        protected void itemGridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}